package com.vagavaga.tcpt;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EventListener;

/**
 * All objects that are interested in {@link IConnection} events, can be
 * notified by registering instance of this interface.
 * 
 * @author river
 * 
 */
public interface IConnectionListener extends EventListener {

    /**
     * Called when connection is first established.
     * 
     * @param conn
     *        connection that has just been established.
     */
    void connected(IConnection conn);

    /**
     * Called when connection has been terminated either by error or calling
     * close method.
     * 
     * @param conn
     *        connection that has been terminated.
     */
    void disconnected(IConnection conn);

    /**
     * Whenever error ocures during accessing raw channel this method will be
     * called with {@link IConnection} instance on witch error occured, and
     * exception that occured.
     * 
     * @param conn
     *        connection that threw an exception
     * @param ex
     *        exception that occured.
     */
    void error(IConnection conn, IOException ex);

    /**
     * Whenever connection receives data it allowes you to read and process that
     * data by calling this method.
     * 
     * @param conn
     *        connection that received data.
     * @param buffer
     *        buffer that contains data. Note that this buffer is normaly shared
     *        with rest of the framework for performance reasons, so cashing it
     *        will do no good.
     */
    void dataReceived(IConnection conn, ByteBuffer buffer);
}
